package com.example.demo.service.impl;

import com.example.demo.dataobject.Insulin;
import com.example.demo.dataobject.MedicineShorthand;
import com.example.demo.dataobject.OralMedicine;
import com.example.demo.dataobject.UserInfo;
import com.example.demo.repository.MedicineShorthandRepository;
import com.example.demo.repository.UserInfoRepository;
import com.example.demo.service.InsulinService;
import com.example.demo.service.MedicineShorthandService;
import com.example.demo.service.OralMedicineService;
import jdk.nashorn.internal.runtime.regexp.joni.constants.RegexState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
@Slf4j
public class MedicineShorthandServiceImpl implements MedicineShorthandService {

    @Autowired
    private UserInfoRepository userInfoRepository;

    @Autowired
    private InsulinService insulinService;

    @Autowired
    private OralMedicineService oralMedicineService;

    @Autowired
    private MedicineShorthandRepository medicineShorthandRepository;

    @Override
    public MedicineShorthand save(String telephone, String insulinName, Integer insulinDose, String oralMedicineName, Integer oralMedicineDose, String remarks, Timestamp recordTime) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);

        Insulin insulin = insulinService.findByName(insulinName);
        OralMedicine oralMedicine = oralMedicineService.findByName(oralMedicineName);

        MedicineShorthand medicineShorthand = new MedicineShorthand(userInfo.getId(), insulin.getId(), insulinName,insulinDose, oralMedicine.getId(), oralMedicineName,oralMedicineDose, remarks, recordTime);
        MedicineShorthand result = medicineShorthandRepository.save(medicineShorthand);
        return result;
    }

    @Override
    public List<MedicineShorthand> findByTelephoneOrderByRecordTimeDesc(String telephone) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);
        List<MedicineShorthand> result = medicineShorthandRepository.findByUserIdOrderByRecordTimeDesc(userInfo.getId());
        return result;
    }

    @Override
    public MedicineShorthand saveModification(Integer id, String telephone, String insulinName, Integer insulinDose, String oralMedicineName, Integer oralMedicineDose, String remarks, Timestamp recordTime) {
        UserInfo userInfo = userInfoRepository.findByTelephone(telephone);

        Insulin insulin = insulinService.findByName(insulinName);
        OralMedicine oralMedicine = oralMedicineService.findByName(oralMedicineName);

        MedicineShorthand medicineShorthand = new MedicineShorthand(id,userInfo.getId(), insulin.getId(), insulinName,insulinDose, oralMedicine.getId(), oralMedicineName,oralMedicineDose, remarks, recordTime);
        MedicineShorthand result = medicineShorthandRepository.save(medicineShorthand);

        return result;
    }

    @Override
    public Boolean delete(Integer id) {
        medicineShorthandRepository.delete(id);
        MedicineShorthand result = findById(id);
        if (result == null) {
            return true;
        }
        return false;
    }

    @Override
    public MedicineShorthand findById(Integer id) {
        MedicineShorthand result = medicineShorthandRepository.findById(id);
        return result;
    }
}
